package com.ccp.dev.qbdms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.enums.FlowStatusEnum;
import com.ccp.dev.qbdms.expimp.util.AbstractExpUtil;
import com.ccp.dev.qbdms.expimp.util.AuxiliaryUtil;
import com.ccp.dev.qbdms.model.WQbdProductCertificateInfo;
import com.ccp.dev.qbdms.model.WQbdXhdh;
import com.ccp.dev.qbdms.service.WCustomColumnsService;
import com.ccp.dev.qbdms.service.WQbdProductCertificateInfoService;
import com.ccp.dev.qbdms.service.WQbdXhdhService;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.StructureModifyUtil;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 产品证书信息 信息操作处理控制器
 *
 * @author milihao
 * @date 2020-10-19 15:36:00
 */
@Controller
/**
 * 请将此名称添加到日志开关
 */
@Action(customModel = "产品证书信息")
@RequestMapping("/qbdms/wQbdProductCertificateInfo")
public class WQbdProductCertificateInfoController extends SysBaseController {

	@Resource
	private WQbdProductCertificateInfoService wQbdProductCertificateInfoService;
	@Resource
	private WCustomColumnsService wCustomColumnsService;

	@Resource
	private WQbdXhdhService wQbdXhdhService;

	/**
	 * 功能描述: 跳转到首页
	 *
	 * @return: java.lang.String
	 * @auther: milihao
	 * @date: 2020-10-19 15:36:00
	 */
	@RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
	@Action(description = "跳转到产品证书信息首页", exectype = "页面跳转日志")
	public String viewList(HttpServletRequest request, ModelMap modelMap) {
		wCustomColumnsService.addCustomCols(request);
		// 当前系统等级
		BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
		modelMap.put("currentSystemLevel", currentSystemLevel);
		// 当前登录用户id
		modelMap.put("currentUserId", ContextUtil.getCurrentUserId());
		setModelMap(modelMap);
		return "wQbdProductCertificateInfo/list";
	}

	/**
	 * 功能描述: 附带检索条件的分页查询
	 *
	 * @param: [request] http请求
	 * @return: java.lang.Object
	 * @auther: milihao
	 * @date: 2020-10-19 15:36:00
	 */
	@RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
	@ResponseBody
	@Action(description = "附带检索条件的分页查询产品证书信息", detail = "查询数据信息<#if success>成功<#else></#if>")
	public Object list(HttpServletRequest request) throws IllegalAccessException {
		QueryFilter queryFilter = new QueryFilter(request, true);
		// 添加通用查询条件
		ZhiLiangConst.addQueryFilter(queryFilter);
		//分页数据
		List<WQbdProductCertificateInfo> pageDatas = wQbdProductCertificateInfoService
				.queryList(queryFilter);
		// 密级、上报状态、数据来源单位显示中文
		ZhiLiangConst.showName(pageDatas);

		SysAuditThreadLocalHolder.putParamerter("success", true);
		return success(pageDatas, queryFilter.getPageBean().getTotalCount());
	}


	/**
	 * 功能描述: 跳转到新增页面
	 *
	 * @return: java.lang.String
	 * @auther: milihao
	 * @date: 2020-10-19 15:36:00
	 */
	@RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
	@Action(description = "跳转到产品证书信息新增页面", exectype = "页面跳转日志")
	public String viewAdd(HttpServletRequest request, ModelMap modelMap) {
		String id = RequestUtil.getString(request, "id");
		modelMap.put("id", id);
		setModelMap(modelMap);

		if (StringUtil.isEmpty(id)) {
			//编号
			String numb =wQbdProductCertificateInfoService.generateNewRegisternum();
			modelMap.put("numb", numb);
			modelMap.put("num", wQbdProductCertificateInfoService.generateNewRegisternumS(numb));
		}

		return "wQbdProductCertificateInfo/add";
	}

	/**
	 * 功能描述:  数据保存通用方法
	 *
	 * @param: wQbdProductCertificateInfo 保存的对象
	 * @return: java.lang.Object
	 * @auther: milihao
	 * @date: 2020-10-19 15:36:00
	 */
	@RequestMapping(UrlConstants.MODULE_FUNCTION_SAVE)
	@ResponseBody
	@Action(description = "保存产品证书信息数据", detail = "<#if isAdd>新增<#else>编辑</#if>产品证书信息数据<#if success>成功<#else>失败</#if><#if StringUtil.isNotEmpty(modifyInfo)>成功></#if>")
	public Object save(@RequestBody WQbdProductCertificateInfo wQbdProductCertificateInfo) {
		String id = wQbdProductCertificateInfo.getId();
		int result;
		if (StringUtil.isEmpty(id)) {
			SysAuditThreadLocalHolder.putParamerter("isAdd", true);
			//新增操作
			//新增数据前生成随机Id
			wQbdProductCertificateInfo.setId(ProjectUUIDUtils.getUUIDFor32());
			ZhiLiangConst.addCommonFields(wQbdProductCertificateInfo);

			wQbdProductCertificateInfo.setTenantId(ContextUtil.getCurrentUserTenantId());
			wQbdProductCertificateInfo.setCreateBy(ContextUtil.getCurrentUserId());
			wQbdProductCertificateInfo.setCreateTime(new Date());

			result = wQbdProductCertificateInfoService.add(wQbdProductCertificateInfo);
		} else {
			SysAuditThreadLocalHolder.putParamerter("success", false);
			//修改操作
			ZhiLiangConst.updateCommonFields(wQbdProductCertificateInfo);

			wQbdProductCertificateInfo.setUpdateBy(ContextUtil.getCurrentUserId());
			wQbdProductCertificateInfo.setUpdateTime(new Date());

			result = wQbdProductCertificateInfoService.update(wQbdProductCertificateInfo);
			SysAuditThreadLocalHolder.putParamerter("modifyInfo", StructureModifyUtil
					.getModifyInfo(wQbdProductCertificateInfoService.getOneById(id),
							wQbdProductCertificateInfo));
		}
		if (result > 0) {
			SysAuditThreadLocalHolder.putParamerter("success", true);
		} else {
			SysAuditThreadLocalHolder.putParamerter("success", false);
		}
		return result > 0 ? getResultDataSuccess("save.success") : getResultDataError("save.failure");
	}

	/**
	 * 功能描述: 跳转到详情页面
	 *
	 * @return: java.lang.String
	 * @auther: milihao
	 * @date: 2020-10-19 15:36:00
	 */
	@RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/particulars")
	@Action(description = "跳转到产品证书信息详情页面", exectype = "页面跳转日志")
	public String viewParticulars(HttpServletRequest request, ModelMap modelMap) {
		String id = RequestUtil.getString(request, "id");
		modelMap.put("id", id);
		String isClose = RequestUtil.getString(request, "isClose");
		modelMap.put("isClose", isClose);
		return "wQbdProductCertificateInfo/particulars";
	}

	/**
	 * 功能描述: 根据主键id获取一条数据的通用查询方法
	 *
	 * @param: id 参数对象必须是"id"
	 * @return: java.lang.Object
	 * @auther: milihao
	 * @date: 2020-10-19 15:36:00
	 */
	@RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
	@ResponseBody
	@Action(description = "根据主键id查询产品证书信息的一条数据", detail = "根据主键id查询产品证书信息的数据<#if success>成功<#else>失败</#if>")
	public Object get(@RequestParam("id") String id) {
		WQbdProductCertificateInfo wQbdProductCertificateInfo = wQbdProductCertificateInfoService
				.getOneById(id);
		if (null == wQbdProductCertificateInfo) {
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("get.failure");
		}
		SysAuditThreadLocalHolder.putParamerter("success", true);
		return getResultDataSuccess("get.success", wQbdProductCertificateInfo);
	}

	/**
	 * 功能描述: 根据主键ID删除数据的通用删除方法
	 *
	 * @param: id 参数名称必须是"id"
	 * @return: java.lang.Object
	 * @auther: milihao
	 * @date: 2020-10-19 15:36:00
	 */
	@PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
	@ResponseBody
	@Action(description = "根据主键ID删除产品证书信息数据", exectype = "删除日志", detail = "删除数据信息<#if success>成功<#else>失败</#if>")
	public Object delete(@RequestParam("id") String id) {
		String userId = ContextUtil.getCurrentUserId();
		if (StringUtil.isEmpty(id)) {
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return fail("delete.failure.message");
		}
		String[] ids = id.split(",");
		for (String key : ids) {
			WQbdProductCertificateInfo wQbdProductCertificateInfo = wQbdProductCertificateInfoService
					.getOneById(key);
			if (!userId.equals(wQbdProductCertificateInfo.getCreateBy())) {
				SysAuditThreadLocalHolder.putParamerter("success", false);
				return fail("删除失败，只能删除自己创建的数据！");
			}

		}
		int result = wQbdProductCertificateInfoService.delByIdArray(ids);
		if (result > 0) {
			SysAuditThreadLocalHolder.putParamerter("success", true);
		} else {
			SysAuditThreadLocalHolder.putParamerter("success", false);
		}
		return result > 0 ? getResultDataSuccess("delete.success")
				: getResultDataError("delete.failure");
	}


	/**
	 * 功能描述: 跳转到导出选择字段页面
	 *
	 * @param: [request] http请求
	 * @param: modelMap
	 * @return: java.lang.String
	 * @auther: milihao
	 * @date: 2020-10-19 15:36:00
	 */
	@RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/export")
	@Action(description = "跳转到产品证书信息导出选择字段页面", exectype = "页面跳转日志")
	public String export(HttpServletRequest request, ModelMap modelMap) throws Exception {
		com.alibaba.fastjson.JSONObject jsonObject = AuxiliaryUtil
				.resolverModel(WQbdProductCertificateInfo.class);
		modelMap.put("tableInfo", jsonObject.toString());
		return "expImp/add";
	}

	/**
	 * 功能描述: 导出产品证书信息数据信息
	 *
	 * @param: [request] http请求
	 * @return: java.lang.Object
	 * @auther: milihao
	 * @date: 2020-10-19 15:36:00
	 */
	@RequestMapping("/exportData")
	@Action(description = "导出产品证书信息数据信息", detail = "导出产品证书信息数据信息<#if success>成功<#else></#if>")
	public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SysAuditThreadLocalHolder.putParamerter("success", false);
		String[] id = RequestUtil.getStringAryByStr(request, "id");
		QueryFilter queryFilter = new QueryFilter(request, false);
		JSONArray dataArray = ZhiLiangConst.exportAddConditions(queryFilter, id);
		List<WQbdProductCertificateInfo> list = this.wQbdProductCertificateInfoService
				.queryAll(queryFilter);
		if (list.isEmpty()) {
			list.add(new WQbdProductCertificateInfo());
		}
		new AbstractExpUtil() {
			@Override
			public String customAnalysis(String fieldName, String fieldValue) {
				return null;
			}
		}.executeExport(request, response, list, dataArray, AbstractExpUtil.ExpStyle.MANY_SHEET);
		SysAuditThreadLocalHolder.putParamerter("success", true);
	}

	private void setModelMap(ModelMap modelMap) {
		List<WQbdXhdh> wQbdXhdhs = wQbdXhdhService.getAll();
		modelMap.put("wQbdXhdhs", JSON.toJSONString(wQbdXhdhs));
	}
}
